package com.inei.cie.capitulovii;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIIDao;
import com.inei.cie.entidades.P7;

public class CapituloVII_SeccionA_Estado_De_Los_Porticos_De_Concreto_Activity
		extends Activity {

	// Componentes
	public TableLayout tl7A;
	public TableRow tr7A9, tr7A9A, tr7A9B, tr7A9C, tr7A9D;
	public Spinner sp7A5, sp7A6, sp7A7, sp7A8, sp7A9A, sp7A9B, sp7A9C, sp7A9D;
	public RadioGroup rg7A9, rg7A10, rg7A11;
	public RadioButton rb7A1, rb7A2, rb7A3, rb7A10si, rb7A10no, rb7A11si,
			rb7A11no;
	public TextView tvEdificacionNro;

	// Data
	public CapituloVIIDao cap7dao;
	public String id_local;
	public int Nro_Ed;
	public int Nro_Pred;
	public String edificacion;
	public P7 obj;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_vii_seccion_a_estado_de_los_porticos_de_concreto);

		IniciarComponentes();

		rg7A9.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb7A1:
					if (rb7A1.isChecked()) {
						tl7A.removeView(tr7A9A);
						tl7A.removeView(tr7A9B);
						tl7A.removeView(tr7A9C);
						tl7A.removeView(tr7A9D);
						sp7A9C.setSelection(0);
						sp7A9D.setSelection(0);
						tl7A.addView(tr7A9A, tl7A.indexOfChild(tr7A9) + 1);
						tl7A.addView(tr7A9B, tl7A.indexOfChild(tr7A9) + 2);
					}
					break;

				case R.id.rb7A2:
					if (rb7A2.isChecked()) {
						tl7A.removeView(tr7A9A);
						tl7A.removeView(tr7A9B);
						tl7A.removeView(tr7A9C);
						tl7A.removeView(tr7A9D);
						sp7A9A.setSelection(0);
						sp7A9B.setSelection(0);
						tl7A.addView(tr7A9C, tl7A.indexOfChild(tr7A9) + 1);
						tl7A.addView(tr7A9D, tl7A.indexOfChild(tr7A9) + 2);
					}
					break;

				case R.id.rb7A3:
					tl7A.removeView(tr7A9A);
					tl7A.removeView(tr7A9B);
					tl7A.removeView(tr7A9C);
					tl7A.removeView(tr7A9D);
					sp7A9A.setSelection(0);
					sp7A9B.setSelection(0);
					sp7A9C.setSelection(0);
					sp7A9D.setSelection(0);
					break;

				default:
					break;
				}
			}
		});
	}

	public void IniciarComponentes() {
		// Componentes
		tvEdificacionNro = (TextView) findViewById(R.id.tvEdificacionNro);
		tl7A = (TableLayout) findViewById(R.id.tl7A);
		tr7A9 = (TableRow) findViewById(R.id.tr7A9);
		tr7A9A = (TableRow) findViewById(R.id.tr7A9A);
		tr7A9B = (TableRow) findViewById(R.id.tr7A9B);
		tr7A9C = (TableRow) findViewById(R.id.tr7A9C);
		tr7A9D = (TableRow) findViewById(R.id.tr7A9D);

		sp7A5 = (Spinner) findViewById(R.id.sp7A5);
		sp7A6 = (Spinner) findViewById(R.id.sp7A6);
		sp7A7 = (Spinner) findViewById(R.id.sp7A7);
		sp7A8 = (Spinner) findViewById(R.id.sp7A8);
		sp7A9A = (Spinner) findViewById(R.id.sp7A9A);
		sp7A9B = (Spinner) findViewById(R.id.sp7A9B);
		sp7A9C = (Spinner) findViewById(R.id.sp7A9C);
		sp7A9D = (Spinner) findViewById(R.id.sp7A9D);

		rg7A9 = (RadioGroup) findViewById(R.id.rg7A9);
		rb7A1 = (RadioButton) findViewById(R.id.rb7A1);
		rb7A2 = (RadioButton) findViewById(R.id.rb7A2);
		rb7A3 = (RadioButton) findViewById(R.id.rb7A3);
		rg7A10 = (RadioGroup) findViewById(R.id.rg7A10);
		rb7A10si = (RadioButton) findViewById(R.id.rb7A10si);
		rb7A10no = (RadioButton) findViewById(R.id.rb7A10no);
		rg7A11 = (RadioGroup) findViewById(R.id.rg7A11);
		rb7A11si = (RadioButton) findViewById(R.id.rb7A11si);
		rb7A11no = (RadioButton) findViewById(R.id.rb7A11no);

		tl7A.removeView(tr7A9A);
		tl7A.removeView(tr7A9B);
		tl7A.removeView(tr7A9C);
		tl7A.removeView(tr7A9D);

		cargarData();
	}

	public void cargarData() {
		// Data
		id_local = getIntent().getExtras().getString("id_local");
		Nro_Ed = getIntent().getExtras().getInt("Nro_Ed");
		Nro_Pred = getIntent().getExtras().getInt("Nro_Pred");
		edificacion = "Local : "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - Edificaci\u00f3n Nro. " + (Nro_Ed < 10 ? "0" : "")
				+ Nro_Ed;

		tvEdificacionNro.setText(edificacion);

		cap7dao = new CapituloVIIDao(getApplicationContext());
		obj = cap7dao.obtenerP7Edificacion(id_local, Nro_Ed, Nro_Pred);

		if (obj.getP7_1_5() != 0) {
			sp7A5.setSelection(obj.getP7_1_5());
		}

		if (obj.getP7_1_6() != 0) {
			sp7A6.setSelection(obj.getP7_1_6());
		}

		if (obj.getP7_1_7() != 0) {
			sp7A7.setSelection(obj.getP7_1_7());
		}

		if (obj.getP7_1_8() != 0) {
			sp7A8.setSelection(obj.getP7_1_8());
		}

		if (obj.getP7_1_9() != 0) {
			if (obj.getP7_1_9() == 1) {
				rb7A1.setChecked(true);
				tl7A.removeView(tr7A9A);
				tl7A.removeView(tr7A9B);
				tl7A.removeView(tr7A9C);
				tl7A.removeView(tr7A9D);
				tl7A.addView(tr7A9A, tl7A.indexOfChild(tr7A9) + 1);
				tl7A.addView(tr7A9B, tl7A.indexOfChild(tr7A9) + 2);
			} else {
				if (obj.getP7_1_9() == 2) {
					rb7A2.setChecked(true);
					tl7A.removeView(tr7A9A);
					tl7A.removeView(tr7A9B);
					tl7A.removeView(tr7A9C);
					tl7A.removeView(tr7A9D);
					tl7A.addView(tr7A9C, tl7A.indexOfChild(tr7A9) + 1);
					tl7A.addView(tr7A9D, tl7A.indexOfChild(tr7A9) + 2);
				} else {
					rb7A3.setChecked(true);
					tl7A.removeView(tr7A9A);
					tl7A.removeView(tr7A9B);
					tl7A.removeView(tr7A9C);
					tl7A.removeView(tr7A9D);
				}
			}
		}

		if (obj.getP7_1_9A() != 0) {
			sp7A9A.setSelection(obj.getP7_1_9A());
		}

		if (obj.getP7_1_9B() != 0) {
			sp7A9B.setSelection(obj.getP7_1_9B());
		}

		if (obj.getP7_1_9C() != 0) {
			sp7A9C.setSelection(obj.getP7_1_9C());
		}

		if (obj.getP7_1_9D() != 0) {
			sp7A9D.setSelection(obj.getP7_1_9D());
		}

		if (obj.getP7_1_10() != 0) {
			if (obj.getP7_1_10() == 1) {
				rb7A10si.setChecked(true);
			} else {
				rb7A10no.setChecked(true);
			}
		}

		if (obj.getP7_1_11() != 0) {
			if (obj.getP7_1_11() == 1) {
				rb7A11si.setChecked(true);
			} else {
				rb7A11no.setChecked(true);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capitulovii, menu);
		menu.getItem(0).setTitle("EDIFICACIONES");
		menu.getItem(1).setTitle("OPINI\u00d3N T\u00c9CNICA");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this, CapituloVII_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			int p_5 = sp7A5.getSelectedItemPosition();
			int p_6 = sp7A6.getSelectedItemPosition();
			int p_7 = sp7A7.getSelectedItemPosition();
			int p_8 = sp7A8.getSelectedItemPosition();
			int p_9 = rb7A1.isChecked() ? 1 : (rb7A2.isChecked() ? 2 : rb7A3
					.isChecked() ? 3 : 0);
			int p_9A = sp7A9A.getSelectedItemPosition();
			int p_9B = sp7A9B.getSelectedItemPosition();
			int p_9C = sp7A9C.getSelectedItemPosition();
			int p_9D = sp7A9D.getSelectedItemPosition();
			int p_10 = rb7A10si.isChecked() ? 1
					: (rb7A10no.isChecked() ? 2 : 0);
			int p_11 = rb7A11si.isChecked() ? 1
					: (rb7A11no.isChecked() ? 2 : 0);

			if (p_9 != 0) {
				if (p_9 == 1) {
					if (p_5 != 0 && p_6 != 0 && p_7 != 0 && p_8 != 0
							&& p_9 != 0 && p_9A != 0 && p_9B != 0 && p_9C == 0
							&& p_9D == 0 && p_10 != 0 && p_11 != 0) {
						obj.setId_local(id_local);
						obj.setNro_Ed(Nro_Ed);
						obj.setP7_1_5(sp7A5.getSelectedItemPosition());
						obj.setP7_1_6(sp7A6.getSelectedItemPosition());
						obj.setP7_1_7(sp7A7.getSelectedItemPosition());
						obj.setP7_1_8(sp7A8.getSelectedItemPosition());
						obj.setP7_1_9(rb7A1.isChecked() ? 1 : (rb7A2
								.isChecked() ? 2 : (rb7A3.isChecked() ? 3 : 0)));
						obj.setP7_1_9A(sp7A9A.getSelectedItemPosition());
						obj.setP7_1_9B(sp7A9B.getSelectedItemPosition());
						obj.setP7_1_9C(sp7A9C.getSelectedItemPosition());
						obj.setP7_1_9D(sp7A9D.getSelectedItemPosition());
						obj.setP7_1_10(rb7A10si.isChecked() ? 1 : (rb7A10no
								.isChecked() ? 2 : 0));
						obj.setP7_1_11(rb7A11si.isChecked() ? 1 : (rb7A11no
								.isChecked() ? 2 : 0));
						obj.setNro_Pred(Nro_Pred);
						cap7dao.actualizarP7Edificacion(obj);

						// Salto de vista
						Intent irSeccionB = new Intent(
								CapituloVII_SeccionA_Estado_De_Los_Porticos_De_Concreto_Activity.this,
								CapituloVII_SeccionB_Opinion_Tecnica_Activity.class);
						irSeccionB.putExtras(getIntent().getExtras());
						startActivity(irSeccionB);

						finish();
					} else {
						Toast.makeText(this,
								"Debe responder todas las preguntas",
								Toast.LENGTH_SHORT).show();
					}
				}
				if (p_9 == 2) {
					if (p_5 != 0 && p_6 != 0 && p_7 != 0 && p_8 != 0
							&& p_9 != 0 && p_9A == 0 && p_9B == 0 && p_9C != 0
							&& p_9D != 0 && p_10 != 0 && p_11 != 0) {
						obj.setId_local(id_local);
						obj.setNro_Ed(Nro_Ed);
						obj.setP7_1_5(sp7A5.getSelectedItemPosition());
						obj.setP7_1_6(sp7A6.getSelectedItemPosition());
						obj.setP7_1_7(sp7A7.getSelectedItemPosition());
						obj.setP7_1_8(sp7A8.getSelectedItemPosition());
						obj.setP7_1_9(rb7A1.isChecked() ? 1 : (rb7A2
								.isChecked() ? 2 : (rb7A3.isChecked() ? 3 : 0)));
						obj.setP7_1_9A(sp7A9A.getSelectedItemPosition());
						obj.setP7_1_9B(sp7A9B.getSelectedItemPosition());
						obj.setP7_1_9C(sp7A9C.getSelectedItemPosition());
						obj.setP7_1_9D(sp7A9D.getSelectedItemPosition());
						obj.setP7_1_10(rb7A10si.isChecked() ? 1 : (rb7A10no
								.isChecked() ? 2 : 0));
						obj.setP7_1_11(rb7A11si.isChecked() ? 1 : (rb7A11no
								.isChecked() ? 2 : 0));
						obj.setNro_Pred(Nro_Pred);
						cap7dao.actualizarP7Edificacion(obj);

						// Salto de vista
						Intent irSeccionB = new Intent(
								CapituloVII_SeccionA_Estado_De_Los_Porticos_De_Concreto_Activity.this,
								CapituloVII_SeccionB_Opinion_Tecnica_Activity.class);
						irSeccionB.putExtras(getIntent().getExtras());
						startActivity(irSeccionB);

						finish();
					} else {
						Toast.makeText(this,
								"Debe responder todas las preguntas",
								Toast.LENGTH_SHORT).show();
					}
				}
				if (p_9 == 3) {
					if (p_5 != 0 && p_6 != 0 && p_7 != 0 && p_8 != 0
							&& p_9 != 0 && p_9A == 0 && p_9B == 0 && p_9C == 0
							&& p_9D == 0 && p_10 != 0 && p_11 != 0) {
						obj.setId_local(id_local);
						obj.setNro_Ed(Nro_Ed);
						obj.setP7_1_5(sp7A5.getSelectedItemPosition());
						obj.setP7_1_6(sp7A6.getSelectedItemPosition());
						obj.setP7_1_7(sp7A7.getSelectedItemPosition());
						obj.setP7_1_8(sp7A8.getSelectedItemPosition());
						obj.setP7_1_9(rb7A1.isChecked() ? 1 : (rb7A2
								.isChecked() ? 2 : (rb7A3.isChecked() ? 3 : 0)));
						obj.setP7_1_9A(sp7A9A.getSelectedItemPosition());
						obj.setP7_1_9B(sp7A9B.getSelectedItemPosition());
						obj.setP7_1_9C(sp7A9C.getSelectedItemPosition());
						obj.setP7_1_9D(sp7A9D.getSelectedItemPosition());
						obj.setP7_1_10(rb7A10si.isChecked() ? 1 : (rb7A10no
								.isChecked() ? 2 : 0));
						obj.setP7_1_11(rb7A11si.isChecked() ? 1 : (rb7A11no
								.isChecked() ? 2 : 0));
						obj.setNro_Pred(Nro_Pred);
						cap7dao.actualizarP7Edificacion(obj);

						// Salto de vista
						Intent irSeccionB = new Intent(
								CapituloVII_SeccionA_Estado_De_Los_Porticos_De_Concreto_Activity.this,
								CapituloVII_SeccionB_Opinion_Tecnica_Activity.class);
						irSeccionB.putExtras(getIntent().getExtras());
						startActivity(irSeccionB);

						finish();
					} else {
						Toast.makeText(this,
								"Debe responder todas las preguntas",
								Toast.LENGTH_SHORT).show();
					}
				}
			} else {
				Toast.makeText(this, "Debe responder todas las preguntas",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(
					CapituloVII_SeccionA_Estado_De_Los_Porticos_De_Concreto_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this, CapituloVII_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}
}